package com.mf.all.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.all.exception.GraceException;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import com.mf.all.entity.dto.DtoCustomerContacts;
import com.mf.all.entity.req.search.customer.SearchReqCustomerContacts;
import com.mf.all.mapper.MapperCustomerContacts;
import com.mf.all.service.ServiceCustomerContacts;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;


@Service
public class ImplServiceCustomerContacts extends ServiceImpl<MapperCustomerContacts, DtoCustomerContacts> implements ServiceCustomerContacts {


    @Override
    public LambdaQueryWrapper<DtoCustomerContacts> fillParams(SearchReqCustomerContacts params) {
        final LambdaQueryWrapper<DtoCustomerContacts> wrapper = loadQw();

        wrapper.eq(StringUtils.isNotBlank(params.getContactsName()), DtoCustomerContacts::getContactsName, params.getContactsName())
                .eq(StringUtils.isNotBlank(params.getPhone()), DtoCustomerContacts::getPhone, params.getPhone())
                .eq(ObjectUtils.isNotEmpty(params.getIsDefault()), DtoCustomerContacts::getIsDefault, params.getIsDefault())
                .eq(ObjectUtils.isNotEmpty(params.getStatus()), DtoCustomerContacts::getStatus, params.getStatus());
        return wrapper;
    }

    /**
     * 修改
     */
    @Override
    @Transactional
    public void updateContactsById(DtoCustomerContacts dtoCustomerContacts) {
        // 超过24小时后不可在进行修改
        final DtoCustomerContacts customerContacts = getById(dtoCustomerContacts.getId());
        // 获取当前时间和创建时间进行对比
        if (LocalDateTime.now().isAfter(customerContacts.getCreatedTime().plusHours(24))) {
            GraceException.display("超过24不可进行修改,如有异常请禁用该数据并重新创建联系方式");
        }
        updateById(dtoCustomerContacts);
    }
}